package com.appbee.cm.client.modular.presenter;

import javax.inject.Inject;

import com.appbee.core.client.modular.IModul;
import com.appbee.core.client.modular.impl.ModulRegistry;
import com.google.gwt.event.shared.EventBus;

public class ModulSelectionBoxPresenter
{
	private View view;
	private final SelectView selectView;
	private final ModulRegistry modulRegistry;
	private IModul value = null;
	private final EventBus eventBus;

	public interface View
	{
		public void setValue(IModul value);
	}

	public interface SelectView
	{
		public void setPresenter(ModulSelectionBoxPresenter presenter);

		public void show(Object source);

		public void hide();

		public void setSelectedValue(IModul value);
	}

	@Inject
	public ModulSelectionBoxPresenter(	ModulRegistry modulRegistry,
										EventBus eventBus,
										SelectView selectView)
	{
		this.modulRegistry = modulRegistry;
		this.eventBus = eventBus;
		this.selectView = selectView;
		selectView.setPresenter(this);

	}

	public void setValue(IModul modul)
	{
		this.value = modul;
		view.setValue(modul);
	}
	public void onModulSelected(IModul modul)
	{
		this.value = modul;
		selectView.hide();
		view.setValue(modul);
		eventBus.fireEventFromSource(new ModulSelectionEvent(value), view);
	}

	public void setView(View view)
	{
		this.view = view;
	}

	public void onSelectClick(Object source)
	{
		selectView.show(source);
		selectView.setSelectedValue(value);
	}

	public IModul getValue()
	{
		return value;
	}

	public void setModulIdValue(String modulId)
	{
		value = modulRegistry.getById(modulId);
	}
}
